import { Component, OnInit, Output, EventEmitter } from '@angular/core';

import { InitService } from 'app/public/serve/init/init.service';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from 'app/public/serve/hints/hints.service';
import { Service } from 'app/server/server';

import { scope } from '../scope.data';
import { scopeUrl } from '../scope.url';

declare var $: any;

@Component({
  selector: 'scopes-newly-added',
  templateUrl: './scopes-newly-added.component.html',
  styleUrls: ['./scopes-newly-added.component.css']
})

export class ScopesNewlyAddedComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();
  
  serviceTypeInfo   : Array<object>;

  constructor(
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService,
    private server: Service
  ) { }

  ngOnInit() {
  }

  /**
   * 服务类型
   */ 
  _serviceType() {
    this.serviceTypeInfo = scope.serviceType;
  }

  /**
   * 服务类型（中文名称）
   */ 
  _serviceTypeName() {
    return $('#serviceType').text();
  }

  /**
   * 新增
   * @param info 
   */ 
  _onSubmit(info: object) {

    info['serviceTypeName'] = this._serviceTypeName();
 
    this.request
        .postRequest(this.server.baseUrl + scopeUrl.add, info)
        .then(res=>{
          if(res.result_code == '0000') {
            this.onSubmit.emit();
          }
          this.hintsService._operationHints(res.message, res.result_code);
          this._init();
          return
        })
        .catch(err=> {
          this.hintsService._operationHints('error', 'error');
          this._init();
          return
        })
  }

  /**
   * 初始化弹窗
   */
  private _init() {
    this.initService._init();
    $('#newlyAdded').modal('hide');
  }

}
